import logging
import os

class LoggingHandler:
    def __init__(self, log_file="application.log"):
        self.logger = logging.getLogger("my_query_project")
        self.logger.setLevel(logging.INFO)

        # 检查日志记录器是否已经有了处理器
        if not self.logger.hasHandlers():
            # 创建文件处理器
            file_handler = logging.FileHandler(log_file)
            file_handler.setLevel(logging.INFO)

            # 创建控制台处理器
            console_handler = logging.StreamHandler()
            console_handler.setLevel(logging.INFO)

            # 创建格式化器并将其添加到处理器
            formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
            file_handler.setFormatter(formatter)
            console_handler.setFormatter(formatter)

            # 将处理器添加到记录器
            self.logger.addHandler(file_handler)
            self.logger.addHandler(console_handler)

    def get_logger(self):
        return self.logger

